package com.mszl.blog.controller;

import com.mszl.blog.entity.Tag;
import com.mszl.blog.service.TagService;
import com.mszl.blog.vo.Result;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("tags")
public class TagController {
    @Resource
    private TagService tagService;
    //最热标签
    @GetMapping("hot")
    public Result hot(){
        int limit=6;
        return tagService.hots(limit);

    }
    //所有的标签
    @GetMapping("all")
    public Result listCategory(){
        return tagService.findAll();
    }

    //根据tag_id查找详细信息
    @GetMapping("detail/{id}")
    public Result findADetailById(@PathVariable("id") Long id) {
        return tagService.findADetailById(id);

    }






    @GetMapping
    public ResponseEntity<Page<Tag>> queryByPage(Tag tag, PageRequest pageRequest) {
        return ResponseEntity.ok(this.tagService.queryByPage(tag, pageRequest));
    }

    @GetMapping("{id}")
    public ResponseEntity<Tag> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.tagService.queryById(id));
    }

    @PostMapping("addTag")
    public ResponseEntity<Tag> add(@RequestBody Tag tag) {
        return ResponseEntity.ok(this.tagService.insert(tag));
    }

    @PutMapping
    public ResponseEntity<Tag> edit(@RequestBody Tag tag) {
        return ResponseEntity.ok(this.tagService.update(tag));
    }

    @DeleteMapping("{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable Long id) {
        return ResponseEntity.ok(this.tagService.deleteById(id));
    }
}
